package com.anda.logistics.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class JQGridJsonData {
	private long page;
	private long total;
	private long records;
	private List<JQGridJsonDataRow> rows;
	
	public long getPage() {
		return page;
	}
	public void setPage(long page) {
		this.page = page;
	}
	public long getTotal() {
		return total;
	}
	public void setTotal(long total) {
		this.total = total;
	}
	public long getRecords() {
		return records;
	}
	public void setRecords(long records) {
		this.records = records;
	}
	public List<JQGridJsonDataRow> getRows() {
		return rows;
	}
	public void setRows(List<JQGridJsonDataRow> rows) {
		this.rows = rows;
	}
	
	public void setRowsFromList(List list, String id, String [] cols){
		rows = new ArrayList<JQGridJsonDataRow>();
		int i = 0;
		for(Object o : list){
			JQGridJsonDataRow row = new JQGridJsonDataRow();
			Class clazz = o.getClass();
			try {
				Method method =clazz.getMethod("get"+id.toUpperCase().substring(0,1)+id.substring(1));
				//System.out.println(method.getName());
				//System.out.println(method.invoke(o));
				row.setId( Long.parseLong(method.invoke(o).toString()));
				List cell = new ArrayList();
				for (String col: cols){
					method =clazz.getMethod("get"+col.toUpperCase().substring(0,1)+col.substring(1));
					cell.add(method.invoke(o));
				}
				row.setCell(cell);
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			rows.add(row);
		}
	}
	
	
}

